package com.weixin.mw.itcar.common;

import java.io.File;
import java.util.Date;
import java.util.Iterator;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class CreateXMLString {

	public static final File SEND_STRING_FILE = new File(CreateXMLString.class.getResource(
			"/").getPath()
			+ "template/SendText.xml");

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Date date = new Date();
		String createTime=String.valueOf(date.getTime());
		SendXML("weixin", "mengwei", createTime, "MENGWEI", "");
	}

	public static String SendXML(String toUserName, String fromUserName,
			String createTime, String content, String msgId) {

		Document doc;
		try {
			SAXReader reader = new SAXReader();
			doc = reader.read(SEND_STRING_FILE);
			Element rootElt = doc.getRootElement();
			for (Iterator i = rootElt.elementIterator(); i.hasNext();) {

				Element element = (Element) i.next();

				if ("ToUserName".equals(element.getName())) {
					element.addCDATA(toUserName);;

				}else if("FromUserName".equals(element.getName())) {
					element.addCDATA(fromUserName);;

				}else if("CreateTime".equals(element.getName())) {
					element.setText(createTime);;
				}else if("MsgType".equals(element.getName())) {
					element.addCDATA("text");;

				}else if("Content".equals(element.getName())) {
					element.addCDATA(content);;

				}else if("MsgId".equals(element.getName())) {
					element.setText("dd");

				}
//				Element element = rootElt.element("Content");
//				// rootElt.attribute(5).setText("eeeee");
//				element.addCDATA(Content);
//				
			}
			System.out.println(doc.asXML().toString());
		} catch (DocumentException e) {
			e.printStackTrace();
		}
		return msgId;
	}
}
